package com.lagou;

import com.lagou.io.Resources;
import com.lagou.mapper.UserMapper;
import com.lagou.pojo.User;
import com.lagou.session.SqlSession;
import com.lagou.session.SqlSessionFactory;
import com.lagou.session.SqlSessionFactoryBuilder;
import com.lagou.utils.CamelCaseUtils;
import org.dom4j.DocumentException;

import org.junit.Test;
import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: SAXDemo
 * @Auther: Jerry
 * @Date: 2020/6/19 11:06
 * @Desctiption: TODO
 * @Version: 1.0
 */
public class Demo {

    public SqlSession getSqlSession() throws PropertyVetoException, DocumentException {
        InputStream inputStream = Resources.getResourcesAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession;
    }

    @Test
    public void test1() throws Exception {
        SqlSession sqlSession = getSqlSession();
        User user = new User();
        user.setUsername("admin");
        user.setPassword("123456");
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User obj = userMapper.findUserByCondition(user);
        System.out.println(obj);
    }

    @Test
    public void test2() throws Exception {
        SqlSession sqlSession = getSqlSession();
        User user = new User();
        user.setUsername("jerry");
        user.setPassword("123456a");
        user.setSex("0");
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        int count = userMapper.saveUser(user);
        System.out.println("count:" + count);
    }

    @Test
    public void test3() throws Exception {
        SqlSession sqlSession = getSqlSession();
        User user = new User();
        user.setId(13L);
        user.setUsername("jerry");
        user.setPassword("a123321");
        user.setSex("1");
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        int count = userMapper.updateUser(user);
        System.out.println("count:" + count);
    }


    @Test
    public void test4() throws Exception {
        SqlSession sqlSession = getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> all = userMapper.findAll();
        for (User user : all) {
            System.out.println(user);
        }
    }

    @Test
    public void test5() throws Exception {
        SqlSession sqlSession = getSqlSession();
        // 问题分析：
            // 当调用sqlSession.getMapper，我们指导invoke方法会执行，那invoke去执行的时候，是怎么知道它应该哪个sql语句呢？
            // 执行insert、update、delete时，需不需要返回值？
            // 这里为了方便演示操作数据库是否成功，给个int类型返回值，表示为数据库受影响行数

        // 实现思路：
            // 新建一个枚举类SqlCommandType：INSERT、UPDATE、DELETE、SELECT
            // 对mapper配置文件进行解析时，将mappedStatement对应的操作类型记录下来，比如执行select时，将其转成大写SELECT,存入mappedStatement对象中
            // 执行getMapper时，根据statementId从mappedStatementMap获取mappedStatement，并且将mappedStatement对象中的sql操作类型拿到
            // 对SqlCommandType枚举常量进行遍历匹配
            // 执行相应的sql语句

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        int count = userMapper.deleteById(4L);
//        sqlSession.rollback();
        sqlSession.close();
        System.out.println(count);
    }

    @Test
    public void test6() throws Exception {
        SqlSession sqlSession = getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = new User();
        user.setSex("1");
        int userCount = userMapper.findUserCountByUser(user);
        System.out.println(userCount);
    }

    @Test
    public void test7() throws Exception {
        SqlSession sqlSession = getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        int userCount = userMapper.findUserCount();
        System.out.println(userCount);
    }

    @Test
    public void test8() throws Exception {
        SqlSession sqlSession = getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        Map<String, Object> param = new HashMap<>();
        param.put("username", "admin");
        param.put("password", "123456");
        Map<String, Object> userMap = userMapper.findUserMap(param);
        System.out.println(userMap);
    }

    @Test
    public void test9() throws Exception {
        SqlSession sqlSession = getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        Map<String, Object> param = new HashMap<>();
        param.put("username", "admin");
        param.put("password", "123456");
        List<Map<String, Object>> userList = userMapper.findUserList();
        for (Map<String, Object> map : userList) {
            System.out.println(map);
        }
    }

    public static void main(String[] args) {
        String result = "user_role_id";
        String s = CamelCaseUtils.str2hump(result);
        System.out.println(s);
    }
}
